package optimizers.ganeatgeneric.report;

import util.MyUtil;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import java.util.Map;


import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;



import optimizers.commons.GAIndiv;
import optimizers.commons.RunConfig;

import optimizers.ganeatgeneric.Species;

import visualization.GraphDisplayer;
import visualization.PlotExpData;
import visualization.PlotIndividualHistory;
import visualization.ganeat.PlotSpeciesData;

public class ReaderNEAT {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 */
	public static void openReport(File file) throws ClassNotFoundException {
		FileInputStream saveFile;
		try {
			saveFile = new FileInputStream(file);
			ObjectInputStream restore = new ObjectInputStream(saveFile);
			ArrayList<ArrayList<Species>> speciesVisualData = (ArrayList<ArrayList<Species>>) restore
					.readObject();
			try {

				JFrame graphFrame1 = new JFrame("Species Visualization");
				graphFrame1.setSize(1500, 800);
				GridLayout grid1 = new GridLayout(0, 1);
				JPanel panel1 = new JPanel();
				panel1.setLayout(grid1);
				grid1.setColumns(1);
				graphFrame1.add(panel1);
				//PlotSpeciesData demo4 = new PlotSpeciesData(speciesVisualData,
				//		1500, 800);
				//panel1.add(demo4);
				graphFrame1.setVisible(true);
				double[][] speciesData = (double[][]) restore.readObject();
				String consoleText = (String) restore.readObject();
				Map<String, Integer> innovations = (Map<String, Integer>) restore
						.readObject();
				Map<String, String> nodeNames = (Map<String, String>) restore
						.readObject();
				ArrayList<GAIndiv> speciesLib = (ArrayList<GAIndiv>) restore
						.readObject();
				RunConfig config = (RunConfig) restore.readObject();
				//PlotExpData demo = MyUtil.getSpeciesFitnessPlot(
				//		speciesVisualData, null);
				PlotExpData demo3 = new PlotExpData(
						"Number of species over generations", speciesData,
						new String[] { "nSpecies", "Speciation Threshold" },
						"Generation", "Number of species");
				JFrame graphFrame = new JFrame("Metrics over generations");
				graphFrame.setSize(1200, 800);
				GridLayout grid = new GridLayout(0, 1);
				JPanel panel = new JPanel();
				panel.setLayout(grid);
				grid.setColumns(1);
				graphFrame.add(panel);
				//panel.add(demo);
				panel.add(demo3);
				graphFrame.setVisible(true);
				GraphDisplayer bestIndividuals = new GraphDisplayer(
						"Best Individual for each generations");
				for (int i = speciesVisualData.size() - 1; i < speciesVisualData
						.size(); i++) {
					GAIndiv bestIndividual = null;
					for (Species sp : speciesVisualData.get(i)) {
						if (bestIndividual == null
								|| bestIndividual.getFitness() < sp
										.getBestIndividual().getFitness()) {
							bestIndividual = sp.getBestIndividual();
							bestIndividual.config = config;
						}
					}
					//TODO: correct that
//					bestIndividuals.addGraph(bestIndividual.getOligoSystem()
//							.getVisualization(250, 250), bestIndividual
//							.displayTimeSereis(), bestIndividual
//							.printModelInTextArea());
					bestIndividuals.display();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			restore.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException, IOException {
		// TODO Auto-generated method stub

		 JFileChooser fc = new JFileChooser();
		 int returnVal;
		 do {
		 returnVal = fc.showOpenDialog(null);
		 } while (returnVal != JFileChooser.APPROVE_OPTION
		 && returnVal != JFileChooser.CANCEL_OPTION);
		 File file = fc.getSelectedFile();
//		File file = new File(
//				"C:\\Users\\Huy\\workspace\\SB_Project\\result\\NEAT_Square_2013_04_29 01_15_27");
//		System.out.println(file.toString());
		/*FileInputStream saveFile = new FileInputStream(file);
		ObjectInputStream restore = new ObjectInputStream(saveFile);
		ArrayList<ArrayList<Species>> speciesVisualData = (ArrayList<ArrayList<Species>>) restore
				.readObject();
		JPanel panel = new JPanel();
		JFrame frame = new JFrame("");
		frame.add(panel);
		frame.setSize(1200, 800);
		Individual bestIndividual = null;
		for (Species sp : speciesVisualData.get(speciesVisualData.size() - 1)) {
			if (bestIndividual == null
					|| bestIndividual.getFitness() < sp.getBestIndividual()
							.getFitness()) {
				bestIndividual = sp.getBestIndividual();
			}
		}
		PlotIndividualHistory demo = new PlotIndividualHistory("",
				bestIndividual);
		panel.add(demo);
		frame.setVisible(true);*/
		openReport(file);
	}
}
